﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using GW2LastOnline.Services;

namespace GW2LastOnline
{
    public partial class formLogin : Form
    {
        private bool closed;

        public formLogin()
        {
            InitializeComponent();

            this.DoubleBuffered = true;

            Fonts.Apply(Fonts.Cabin, label4, label5, label6, labelFailed, checkSessionLogin, label7);

            panelSessionKey.Location = panelEmail.Location;
        }

        public CookieCollection Cookies
        {
            get;
            set;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void formLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            closed = true;
        }

        private void OnLoginResult(CookieCollection cookies)
        {
            if (closed)
                return;

            if (cookies == null)
            {
                buttonSignIn.Enabled = true;
                progressLogin.Visible = false;
                pictureExclamation.Visible = labelFailed.Visible = true;
            }
            else
            {
                this.Cookies = cookies;
                this.DialogResult = DialogResult.OK;
            }
        }

        private void buttonSignIn_Click(object sender, EventArgs e)
        {
            if (checkSessionLogin.Checked)
            {
                if (textSessionKey.Text.Length < 36) //8-4-4-4-12
                {
                    pictureExclamation.Visible = labelFailed.Visible = true;
                    textSessionKey.Focus();
                    return;
                }
            }
            else
            {
                if (textUsername.Text.Length == 0 || textPassword.Text.Length == 0)
                {
                    pictureExclamation.Visible = labelFailed.Visible = true;
                    if (textUsername.Text.Length == 0)
                        textUsername.Focus();
                    else
                        textPassword.Focus();
                    return;
                }
            }

            buttonSignIn.Enabled = false;
            progressLogin.Visible = true;
            pictureExclamation.Visible = labelFailed.Visible = false;

            new Thread(new ThreadStart(delegate
                {
                    CookieCollection cookies = null;
                    try
                    {
                        if (checkSessionLogin.Checked)
                            cookies = Account.Login(textSessionKey.Text);
                        else
                            cookies = Account.Login(textUsername.Text, textPassword.Text);
                    }
                    catch { }
                    try
                    {
                        this.BeginInvoke(new MethodInvoker(delegate
                            {
                                OnLoginResult(cookies);
                            }));
                    }
                    catch { }
                })).Start();
        }

        private void formLogin_Load(object sender, EventArgs e)
        {

        }

        private void login_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                e.Handled = true;
                buttonSignIn.Focus();
                buttonSignIn_Click(buttonSignIn, new EventArgs());
            }
        }

        private void checkSessionLogin_CheckedChanged(object sender, EventArgs e)
        {
            pictureExclamation.Visible = labelFailed.Visible = false;
            panelEmail.Visible = !(panelSessionKey.Visible = checkSessionLogin.Checked);

            if (checkSessionLogin.Checked)
            {
                labelFailed.Text = Properties.Resources.login_error_invalid_session;
                textSessionKey.Focus();
            }
            else
            {
                labelFailed.Text = Properties.Resources.login_error_invalid_username_or_password;
                textUsername.Focus();
            }
        }

        private void formLogin_Shown(object sender, EventArgs e)
        {
            textUsername.Focus();
        }
    }
}
